/*
 * 
 * Copyright (C) 2010 Wilfried Elmenreich, Anita Sobe 
 * This file is part of VideoNetwork.
 * 
 * VideoNetwork is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version. 
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. 
 * You should have received a copy of the GNU General Public License along with this program; if not, see <http://www.gnu.org/licenses/>. 
 */
package proxy;

import java.util.Random;

import simulation.SimulatorConfig.ScenarioType;

public class VideoUnit implements Cloneable, Comparable<VideoUnit> {
	private int sizeInBytes;
	// 640x480: 2000 kbits/sec
	// 780p: 9000 kbits/sec
	// QCIF: ca. 200 kbits/sec
	//SOMA LNF: 1000 kbits/sec

	static double videoBitRate = 1000000;
	// static final double videoBitRate = 200000;
	private int id;
	// 5KB to 5MB (should be changed)??
	// LNF:
	// avg 2787607,09
	// stdev 4076724,817
	// min 19620
	// max 45347874
	
	// default values
//	 private static final int minSize = 50000;
//	 private static final int maxSize = 5000000;
//	 static final int avgSize = 500000;
//	 private static final int stdevSize = 500000;

	private static int minSize = 19620;
	private static int maxSize = 15815121;
	static int avgSize = 2414919;
	

	private static int stdevSize = 2692822;
	private Content content;
	private double creationtime;
	/**
	 * The popularity defines the quality of a video in values between 1 and 100
	 * in order to be chosen, a video must have similar content[] as the users
	 * preferences and a sufficient quality
	 */
	public int popularity;

	private double distance;
	private ScenarioType searchScenario;

	public VideoUnit(int id, int contentTypes, Random rand) {
		this(id, contentTypes, rand, ScenarioType.SIMPLEHORMONE);

	}

	public VideoUnit(int id, int contentTypes, Random rand,
			ScenarioType scenario) {

		this.id = id;
		sizeInBytes = (int) (rand.nextGaussian() * stdevSize + avgSize);
		if (sizeInBytes < minSize)
			sizeInBytes = minSize;
		if (sizeInBytes > maxSize)
			sizeInBytes = maxSize;
		content = new Content(contentTypes, rand);
		this.searchScenario = scenario;
		content.fill(searchScenario);

		// CHANGE!! Popularity set in the simulator
		this.popularity = 1;

	}

	public double getCreationtime() {
		return creationtime;
	}

	public void setCreationtime(double creationtime) {
		this.creationtime = creationtime;
	}

	public void calculateDistance(int[] likesContent) {
		distance = Math.sqrt(content.calculateDistance(likesContent))
				/ popularity;
		;
	}

	public void calculateTreeDistance(int[] likesContent) {
		// distance = content.calculateTreeDistance(likesContent)/popularity;
		distance = content.calculateTreeDistance(likesContent) / popularity;
	}

	public int getSizeInBytes() {
		return sizeInBytes;
	}

	public Content getContent() {
		return content;
	}

	public int getPopularity() {
		return popularity;
	}

	public void setPopularity(int popularity) {
		this.popularity = popularity;
	}

	public double getDistance() {
		return distance;
	}

	public int getId() {
		return id;
	}

	public double playLength() {
		return sizeInBytes * 8.0 / videoBitRate;
	}
	
	public static int getMinSize() {
		return minSize;
	}

	public static void setMinSize(int minSize) {
		VideoUnit.minSize = minSize;
	}

	public static int getMaxSize() {
		return maxSize;
	}

	public static void setMaxSize(int maxSize) {
		VideoUnit.maxSize = maxSize;
	}

	public static int getAvgSize() {
		return avgSize;
	}

	public static void setAvgSize(int avgSize) {
		VideoUnit.avgSize = avgSize;
	}

	public static int getStdevSize() {
		return stdevSize;
	}

	public static void setStdevSize(int stdevSize) {
		VideoUnit.stdevSize = stdevSize;
	}

	public static double getVideoBitRate() {
		return videoBitRate;
	}

	public static void setVideoBitRate(double videoBitRate) {
		VideoUnit.videoBitRate = videoBitRate;
	}

	public Object clone() {
		try {
			VideoUnit c = (VideoUnit) super.clone();
			c.content = (Content) content.clone();
			return c;
		} catch (CloneNotSupportedException e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public int compareTo(VideoUnit arg0) {
		return new Double(distance).compareTo(new Double(arg0.getDistance()));
	}
}
